<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018-07-05
 * Time: 11:06
 */
// 开发允许跨域访问
header('Access-Control-Allow-Origin:*');
class Doctor extends MY_Controller
{
    // 医生接单答复
    /**
     *
     */
    public function reply(){
        $co_id = $this->input->post('co_id');
        $do_id = $this->input->post('do_id');
        $reply = $this->input->post('reply');

        if (empty($do_id) || empty($co_id) || empty($reply)){
            $res['start'] = 300;
            $res['content'] = '请完整填写请求内容';
            echo json_encode($res,JSON_UNESCAPED_UNICODE);
            return;
        }

        // 判断是否有医生服务了
        $sql = " SELECT COUNT(*) AS 'count' FROM co_do WHERE co_id = ".$co_id." AND do_id != ".$do_id;
        $queryRes1 = $this->db->query($sql)->row_array();

        if ($queryRes1['count'] > 0){
            $res['start'] = 300;
            $res['data'] = '已经有其他医生答复了';
            echo json_encode($res,JSON_UNESCAPED_UNICODE);
            return ;
        }

        $co_do['co_id'] = $co_id;
        $co_do['do_id'] = $do_id;
        $co_do['reply'] = $reply;
        $co_do['createtime'] = date('Y-m-d h:i:sa',time());

        // 保存记录
        $insertRes = $this->db->insert('co_do', $co_do);


        // 第一次抢单给医生加钱
        $sql = " UPDATE doctor SET money=money+
                (SELECT price FROM conditions WHERE id = ?) WHERE id = ? 
                 AND (SELECT COUNT(*) FROM co_do WHERE do_id = ? AND co_id = ?) = 1 ";
        $this->db->query($sql, array($co_id, $do_id, $do_id, $co_id));

        $res['start'] = 200;
        $res['content'] = '医生答复成功';
        echo json_encode($res,JSON_UNESCAPED_UNICODE);
    }




}